package com.zaofan.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zaofan.bean.AdImg;
import com.zaofan.constant.UrlConstant;
import com.zaofan.dao.AdImgDao;

public class AdImgService {
	
	public List<AdImg> filterByCity(List<AdImg> adImgList, Integer cityYouWante) {
		List<AdImg> result = new ArrayList<>();
		if (cityYouWante == null) {
			return result;
		}
		for(AdImg adimg : adImgList) {
			if (cityYouWante.equals(adimg.getCityId())) {
				result.add(adimg);
			}
		}
		return result;
	}
	
	public AdImg filterByCity(AdImg adImg, Integer cityYouWante) {
		return cityYouWante.equals(adImg.getCityId()) ? adImg: null;
	}
	
	private AdImgDao adImgDao = new AdImgDao();

	public void delete(AdImg adImg) {
		adImgDao.del(adImg);
	}

	public void add(AdImg adImg) {
		adImg.setAddTime(new Date());
		adImgDao.add(adImg);
	}

	public void update(AdImg adImg) {
		adImgDao.update(adImg);
	}

	public List<AdImg> findAll(Map<String, Object> params) {
		List<AdImg> data = adImgDao.findAll(params);
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public Long count(Map<String, Object> params) {
		return adImgDao.count(params);
	}

	public AdImg findById(Long id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return adImgDao.findOne(params);
	}

	public List<AdImg> findByType(String lable) {
		Map<String, Object> params = new HashMap<>();
		params.put("positionLable", lable);
		params.put("state", "0");
		params.put("order", " by sort asc");
		List<AdImg> adImgs = adImgDao.findAll(params);
		for (AdImg img : adImgs) {
			img.setPicture(UrlConstant.HOST_URL + img.getPicture());
		}
		return adImgs;
	}
	
	public AdImg findActivityAd() {
		Map<String, Object> params = new HashMap<>();
		params.put("positionLable", "activityad");
		params.put("state", "0");
		params.put("begin", "0");
		params.put("end", "1");
		params.put("order", " by sort asc");
		AdImg adImgs = adImgDao.findOne(params);
		if(adImgs != null){
			adImgs.setPicture(UrlConstant.HOST_URL + adImgs.getPicture());
		}
		return adImgs;
	}
	
	public AdImg findActivityAd(Integer cityId) {
		Map<String, Object> params = new HashMap<>();
		params.put("positionLable", "activityad");
		params.put("state", "0");
		params.put("begin", "0");
		params.put("cityId", cityId);
		params.put("end", "1");
		params.put("order", " by sort asc");
		AdImg adImgs = adImgDao.findOne(params);
		if(adImgs != null){
			adImgs.setPicture(UrlConstant.HOST_URL + adImgs.getPicture());
		}
		return adImgs;
	}
}
